package com.ks.app.product.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.ks.app.admin.hibernate.KsMerchantRegist;
import com.ks.app.admin.service.MerchantService;
import com.ks.app.admin.util.Pager;
import com.ks.app.logger.Log4jUtil;
import com.ks.app.product.hibernate.KRoom;
import com.ks.app.product.service.KsroomService;
import com.ks.app.product.util.UploadImage;


@ParentPackage("struts-default")
@Namespace("")
@InterceptorRefs( {
	@InterceptorRef(value = "fileUpload", params = {"allowedTypes","image/jpeg,image/gif,image/png","maximumSize", "97867700" }),
	@InterceptorRef("defaultStack")
})  
@Results( {
		@Result(name = "success", location = "/WEB-INF/admin/main.html"),
		@Result(name = "quit", location = "/WEB-INF/admin/send.jsp"),
		@Result(name = "error", location = "/WEB-INF/admin/error.jsp"),
		@Result(name = "roomInfo", location = "/WEB-INF/admin/product/RoomInfo.jsp"),
		@Result(name = "addRoom", location = "/WEB-INF/admin/product/addRoom.jsp"),
		@Result(name = "intiEditRoom", location = "/WEB-INF/admin/product/EditRoom.jsp"),
		@Result(name = "regAdInfo", location = "/WEB-INF/admin/Ad/regAdInfo.jsp"),
		@Result(name = "regaddAd", location = "/WEB-INF/admin/Ad/regaddAd.jsp"),
		@Result(name = "IntiEditregAd", location = "/WEB-INF/admin/Ad/EditregAd.jsp")
})
public class RoomAction implements ServletRequestAware{
	
	private String className = "RoomAction.";
	
	@Resource()
	private KsroomService ksroomServiceimpl;
	@Resource()
	private MerchantService merchantRgService;
	
	private Logger blogger = Log4jUtil.getBusinessLogger();
	
	private HttpServletRequest request;
	private File file;
	private String fileFileName;
	
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	@Resource
	private KsroomService ksroomService;
	
	private KRoom roominfo;
	private String pno;	
	
	
	public String getPno() {
		return pno;
	}
	public void setPno(String pno) {
		this.pno = pno;
	}	
	public void setServletRequest(HttpServletRequest arg0) {
		request=arg0;
	}
	@Action("/admin/product/RoomInfo")
	public String RoomInfo() {	//获得页面参数
		String method = "RoomInfo()";
		blogger.info(className + method +"------come in room info operation---------");
		 try {
			pno = request.getParameter("pno") == null ? "1" : request.getParameter("pno").toString().trim();
			String roommerId=request.getParameter("roommerId")== null ? "" : request.getParameter("roommerId").toString().trim();
			String roomNo=request.getParameter("roomNo")== null ? "" : request.getParameter("roomNo").toString().trim();
			String roomstartdate=request.getParameter("roomstartdate")== null ? "" : request.getParameter("roomstartdate").toString().trim();
			String roomenddate=request.getParameter("roomenddate")== null ? "" : request.getParameter("roomenddate").toString().trim();
			String roomisshow=request.getParameter("roomisshow")== null ? "" : request.getParameter("roomisshow").toString().trim();
			String roomType=request.getParameter("roomType")== null ? "" : request.getParameter("roomType").toString().trim();

			blogger.info(roommerId+"-----"+roomNo+"-----"+roomstartdate+"-----"+roomenddate+"-----"+roomisshow+"-----"+roomType);
			request.setAttribute("roomid", roomNo);
			Pager pager = new Pager();
			int iPageNo = 1;
			try{
				iPageNo = Integer.parseInt(pno);
			}
			catch( Exception e ){        	
			}
			pager.setCntOfPage(20);
			pager.setCurPage(iPageNo);
			List<KRoom> querylist=ksroomService.GetKRoomList(roommerId,roomNo,roomstartdate, roomenddate, roomisshow, pager,roomType);
			request.setAttribute("list_RoomInfo", querylist);
			request.setAttribute("PAGER", pager);
			return "roomInfo";
		}
		 catch (Exception e) 
		 {
			 e.printStackTrace();
			return Error();
		}
	}
	
	public KRoom getRoominfo() {
		return roominfo;
	}
	public void setRoominfo(KRoom roominfo) {
		this.roominfo = roominfo;
	}
	@Action("/admin/product/Error")
	public String Error(){//抛错跳转页面			
		return "error";
	}	
	
	@Action("/admin/product/toaddroom")
	public String toaddAd(){//进入添加房间页面  
		
		List<KRoom> list = ksroomServiceimpl.GetKRoom();
		List<KsMerchantRegist> mers = merchantRgService.getKsMer();
		request.setAttribute("KMers", mers);
		request.setAttribute("KRoom", list);
		return "addRoom";    	
		
    }	
	@Action("/admin/product/saveroom")
	public String addRoom(){//进入添加房间页面  
		
		String method = "addRoom()--";
		blogger.info(className + method + file+"-----------"+fileFileName);
		
		//获取前台传过来的内容
		String roomNo = request.getParameter("room_no");
		String merid = request.getParameter("room_merid");
		String roomType = request.getParameter("room_type");
		String roomStartDate = request.getParameter("room_startdate");
		String roomEndDate = request.getParameter("room_enddate");
		String roomStartTime = request.getParameter("room_starttime");
		String roomEndTime = request.getParameter("room_endtime");
		String roomState = request.getParameter("room_state");
		int roomAmount = Integer.parseInt(request.getParameter("room_amount"));
		
		KsMerchantRegist mer = (KsMerchantRegist) merchantRgService.GetKsMer(merid).get(0);
		KRoom kroom = new KRoom();
		kroom.setRoomAmount(roomAmount);
		kroom.setMerRoom(mer);
		kroom.setRoomEndDate(roomEndDate);
		kroom.setRoomEndTime(roomEndTime);
		kroom.setRoomNo(roomNo);
		kroom.setRoomStartDate(roomStartDate);
		kroom.setRoomStartTime(roomStartTime);
		kroom.setRoomState(roomState);
		kroom.setRoomType(roomType);
		
		//对上传的图片进行处理（存储图片的名字和保存在本地的文件路径）
		String str = new UploadImage().doAddPhoPic(ksroomServiceimpl,file,fileFileName);
		if("success".equals(str)){
			ksroomServiceimpl.addKRoom(kroom);
			return RoomInfo();
		}
		return "error";
	}
	
	@Action("/admin/product/initeditRoom")
	public String initeditroom() {
		
		String method = "initeditroom()--";
		blogger.info(className + method + " come in for initeditroom---");
		
		//取得所有用户对象，用于界面校验用户名和编号是否重复
		List<KRoom> rooms = ksroomServiceimpl.GetKRoom();
		request.setAttribute("list_RoomInfo", rooms);

		String id = request.getParameter("rid");
		List<KRoom> querylist = ksroomServiceimpl.GetKRoom(id);
		blogger.info(className + method + "roomID =" + id +",list.size() = " + querylist.size());
		if (querylist.size() > 0) {
			KRoom room = querylist.get(0);
			request.setAttribute("KRoom", room);
		}
		return "intiEditRoom";
	}	
	@Action("/admin/product/deleteRoom")
	public String deleteAd()//删除房间
	{	 
		String method = "deleteAd()--";
		blogger.info(className + method +"come in to delete roominfo ----------");
			String roomid=request.getParameter("rid");	
			int res=ksroomServiceimpl.deleteKRoom(roomid);
			System.out.println(res);
			if(res>0){
				return RoomInfo();
			}
			else{
				return Error();
			}
	}
}



